package org.example.controller;

import org.example.annotation.TraceLog;
import org.example.service.IArtisanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {



    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private IArtisanService iArtisanService;


    @GetMapping(value = "/hello")
    public String hello(){
        logger.info("请求访问了");
        return "hello world";
    }

    @GetMapping("/testTrace")
    public String testTrace(@RequestParam("name") String name) throws InterruptedException {
        logger.info("testTrace  name={}", name);
        doSomething1();
        // 异步任务
        // artisanService.addArtisan();
        logger.info("testTrace Call Over name={}", name);
        return "Hello," + name;
    }

    private void doSomething1() {
        logger.info("doSomething1  info  log");
        doSomething2();
        logger.error("doSomething1 error log");
    }

    private void doSomething2() {
        logger.info("doSomething2  info log");

    }

    @TraceLog
    @GetMapping("/testTrace2")
    public String testTraceByAnno(@RequestParam("name") String name) throws InterruptedException {
        logger.info("======================");
        logger.info("testTraceByAnno  name={}", name);
        doSomething1();
        //异步方式执行
        iArtisanService.addArtisan();

        logger.info("testTraceByAnno Call Over name={}", name);
        return "Hello," + name;
    }
}
